/*
 * Checkstyle4nb is a plugin for netbeans for checking Java coding conventions.
 * Copyright (C) 2006-2007, Andrea Matsunaga
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.ammatsun.checkstyle4nb.actions;

import org.netbeans.spi.options.OptionsPanelController;

import org.openide.util.HelpCtx;
import org.openide.util.Lookup;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.JComponent;

/**
 * Controls the panel with checkstyle settings, initializaing the panel, and
 * taking care of the ok and cancel buttons actions.
 *
 * @version    1.0, 2006/Jul/12
 * @author     Andrea Matsunaga
 */
public class CheckstyleOptionsPanelController extends OptionsPanelController {
    /** Field description. */
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    /** Field description. */
    private CheckstyleOptionsPanel panel = new CheckstyleOptionsPanel();
    /** Field description. */
    private CheckstyleSettings settings = new CheckstyleSettings();

    /**
     * Creates a new instance of CheckstyleOptionsPanelController.
     */
    public CheckstyleOptionsPanelController() {
    }

    /**
     * Registers new listener.
     * @param l a new listener
     */
    public void addPropertyChangeListener(PropertyChangeListener l) {
        pcs.addPropertyChangeListener(l);
    }

    /**
     * Gets the values set by the user and stores permanently.
     */
    public void applyChanges() {
        settings.put(CheckstyleSettings.CHECKSTYLE_CONFIG,
                panel.getConfigurationFile());
        settings.put(CheckstyleSettings.CHECKSTYLE_PROPERTIES,
                panel.getPropertiesFile());
        settings.storeSettings();
    }

    /**
     * This method is called when Options Dialog "Cancel" button is pressed.
     * This method can be called even before update() method is called.
     */
    public void cancel() {
        // Any possible changes would be reverted here, but since nothing
        // is stored until you press apply, nothing is done
    }

    /**
     * Unregisters given listener.
     * @param l a listener to be removed
     */
    public void removePropertyChangeListener(PropertyChangeListener l) {
        pcs.removePropertyChangeListener(l);
    }

    /**
     * Component should load its data here.
     */
    public void update() {
        // Any time-consuming operations should not be done inside the
        // constructor, because it blocks initialization of OptionsDialog.
        // Initialization should be implemented in update method. This method
        // is called after getComponent(org.openide.util.Lookup) method.
        // Update method can be called more than one time for the same instance
        // of JComponent obtained from getComponent(org.openide.util.Lookup).
        panel.setConfigurationFile((String) settings.get(CheckstyleSettings.CHECKSTYLE_CONFIG));
        panel.setPropertiesFile((String) settings.get(CheckstyleSettings.CHECKSTYLE_PROPERTIES));
    }

    /**
     * Returns visual component representing this options category.
     * This method is called before update() method.
     * @param masterLookup master lookup composed from lookups provided by
     *        individual OptionsPanelControllers
     * @return visual component representing this options category
     */
    public JComponent getComponent(Lookup masterLookup) {
        return panel;
    }

    /**
     * Get current help context associated with this panel.
     * @return current help context
     */
    public HelpCtx getHelpCtx() {
        return new HelpCtx("com.ammatsun.checkstyle4nb.actions.CheckstyleOptionsPanelController");
    }

    /**
     * Should return true if some option value in this category has been changed.
     * @return true if some option value in this category has been changed,
     *         false otherwise
     */
    public boolean isChanged() {
        return (panel.getConfigurationFile().equals(settings.get(CheckstyleSettings.CHECKSTYLE_CONFIG))
                || panel.getPropertiesFile().equals(settings.get(CheckstyleSettings.CHECKSTYLE_PROPERTIES)));
    }

    /**
     * Should return true if some option value in this category is valid.
     * @return true if option values in this category is valid, false otherwise
     */
    public boolean isValid() {
        return panel.valid();
    }
}
